<?php
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

namespace App\Repositories;


use App\Cart;
use App\CartCoupon;
use App\Coupon;
use App\Exceptions\CartExceptions;
use App\Good;
use App\Interfaces\CartService;
use PhpParser\Node\Expr\Array_;

class CartRepository implements CartService
{
    protected $user;

    public function __construct()
    {
        $this->user = \Auth::user();
    }

    /**
     * 获取购物车商品
     * @param string $userId
     * @param string $session_id
     * @return mixed
     */
    public function getCartGoods()
    {
        $cart = Cart::with('good')->where('user_id', $this->user->id)->get()->toArray();

    }

    public function deleteCartGoods(array $goods_ids=[])
    {

        $cart= Cart::where('user_id', $this->user->id);
        if(!empty($goods_ids)){
          $cart= $cart->whereIn('goods_id',$goods_ids);
        }
        return   $cart=$cart->delete();
    }

    /**
     * @param $goods_id
     * @param $goods_number
     *
     */
    public function updateGoods($goods_id, $goods_number)
    {
        $good = Good::find($goods_id);

        if (!$good) {
            throw  new CartExceptions('没有此商品信息');
        }
        //查询购物车有么有此商品
        $cart = Cart::where('user_id', $this->user->id)->where('goods_id', $goods_id)->first();
        if (!$cart) {
            $cart=new Cart();
        }

        //判断库存
        if ($good->stock <  $goods_number) {
            throw new CartExceptions($good->goods_name.'库存不足');
        }

        if ($cart) {//
            $cart->buy_num = $goods_number;
            $cart->save();
        }

        return true;
    }

    /**
     * 添加商品到购物车
     * @param $goods_id
     * @param $goods_number
     * @return mixed
     */
    public function addGoods($goods_id, $goods_number)
    {
        $good = Good::find($goods_id);
        if (!$good) {
            throw  new CartExceptions('没有此商品信息');
        }
        //查询购物车有么有此商品
        $cart = Cart::where('user_id', $this->user->id)->where('goods_id', $goods_id)->first();
        if (!$cart) {
            $buy_num = 0;
        } else {
            $buy_num = $cart->buy_num;
        }
        //判断库存
        if ($good->stock < $buy_num + $goods_number) {
            throw new CartExceptions('库存不足');
        }
        if ($cart) {//数量相加
            $cart->buy_num = $cart->buy_num + $goods_number;
            $cart->save();
        } else {
            $cart = new Cart();
            $cart->user_id = $this->user->id;
            $cart->goods_id = $goods_id;
            $cart->buy_num = $goods_number;
            $cart->save();
        }
        return true;
    }

    /**
     * 购物车绑定优惠券
     *
     */
    public function bindCoupon(Coupon $coupon)
    {
        $cartConPon = CartCoupon::where('user_id', $this->user->id)
            ->where('coupon_id', $coupon->id)->first();
        if (!$cartConPon) {
            $cartConPon = new CartCoupon();
        }
        $cartConPon->coupon_id = $coupon->id;
        $cartConPon->user_id = $this->user->id;
        $cartConPon->save();
        return true;
    }

    /**
     * 删除购物车中某个商品
     * @param $goods_id
     * @return mixed
     */
    public function removeGoods(Array $goods_ids)
    {
        return Cart::whereIn('goods_id', $goods_ids)->delete();

    }



    /**
     * 获取购物车详情
     * @param array $goods_id
     * @return mixed 1. 总价,总个数,总关税
     */

    //public function getInfo(Array $cartGoods, $sel_id);
    public function getInfo(Array $goods_ids = [])
    {
        $cart = ['price' => 0, 'cartGoods' => ''];
        $cartGoods = Cart::join('goods', 'goods.id', '=', 'cart.goods_id')->where('cart.user_id', $this->user->id);
        if (!empty($goods_ids)) {
            $cartGoods = $cartGoods->whereIn('cart.goods_id', $goods_ids);
        }
        $cartGoods = $cartGoods->get()->toArray();
        //计算总价格
        foreach ($cartGoods as &$v) {
            $v['goods_thumbnail']=get_image($v['goods_thumbnail']);
            //判断商品价格是否是首购
            if ($this->user->is_recharge) {
                $cart['price'] += $v['repeat_price'] * $v['buy_num'];
            } else {
                $cart['price'] += $v['cost_price'] * $v['buy_num'];
            }
        }
        $cart['cartGoods'] = $cartGoods;
        return $cart;
    }

    /**
     * 游客用户转化为登陆用户
     * @param $session_id
     * @param $user_id
     * @return mixed
     */
    public function linkSessionToUser($session_id, $user_id)
    {

    }


    /**
     * 获取商品数组中商品总数、总价、总关税等信息
     * @param array $cartGoods :已选中商品数组array(0=>array('goods_id'=>1, 'goods_num'=>2), 1=>array('goods_id'=>2, 'goods_num'=>2))
     * @return mixed 1. 总价,总个数,总关税
     */
    public function getGoodsInfo($cartGoods)
    {


    }//end func getGoodsInfo

    public function getCartNumber()
    {
        return Cart::where('user_id', $this->user->id)->sum('buy_num');

    }

    /**
     * 过滤购物车参数
     * @param $cartGoods
     * @return \Illuminate\Support\Collection
     */
    private function fillterCartGoods($cartGoods)
    {

        //取出该用户的所有购买商品ID
        $cartIds = Cart::select('goods_id')->where($this->getTypeFiled(), $this->reference_id)->get()->lists('goods_id')->toArray();

        $fillterCartGoods = [];

        foreach ($cartGoods as $k => $v) {

            if (in_array($v['goods_id'], $cartIds)) {//判断传来的goods_id参数是否在购物车

                $fillterCartGoods[$v['goods_id']] = $v['buy_num'];

            }
        }

        return $fillterCartGoods;
    }

}